package ywpt.service.service;

import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientDevice;
import ywpt.model.ClientSoft;
import ywpt.model.CommonTable;
import ywpt.model.JobInfo;
import ywpt.model.JobProcInfo;
@Service("jobProcInfoService")
@RemoteProxy(name="jobProcInfoService")
public class JobProcInfoServiceImpl extends HibernateEntityDao<JobProcInfo> implements JobProcInfoService{

	@Override
	public JobProcInfo getJobProcInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public void removeJobProcInfoById(Long id) {
		JobProcInfo jobProcInfo=this.get(id);
		jobProcInfo.setDeleteFlag(true);
		this.update(jobProcInfo);
	}

	@Override
	public void saveJobProcInfo(JobProcInfo jobProcInfo, Long jobId,
			Long softId, Long deviceId, Long evaluationId) {
		if (jobId!=null) {
			jobProcInfo.setJobInfo(this.get(JobInfo.class, jobId));
		}
		if (softId!=null) {
			jobProcInfo.setClientSoft(this.get(ClientSoft.class, softId));
		}
		if (deviceId!=null) {
			jobProcInfo.setClientDevice(this.get(ClientDevice.class, deviceId));
		}
		if (evaluationId!=null) {
			jobProcInfo.setEvaluation(this.get(CommonTable.class, evaluationId));
		}
		this.save(jobProcInfo);
		
	}

	@Override
	public void updateJobProcInfo(JobProcInfo jobProcInfo, Long jobId,
			Long softId, Long deviceId, Long evaluationId) {
		if (jobId!=null) {
			jobProcInfo.setJobInfo(this.get(JobInfo.class, jobId));
		}
		if (softId!=null) {
			jobProcInfo.setClientSoft(this.get(ClientSoft.class, softId));
		}
		if (deviceId!=null) {
			jobProcInfo.setClientDevice(this.get(ClientDevice.class, deviceId));
		}
		if (evaluationId!=null) {
			jobProcInfo.setEvaluation(this.get(CommonTable.class, evaluationId));
		}
		this.update(jobProcInfo);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<JobProcInfo> getHistoryInfo(Long softId, Long hardId) {
		DetachedCriteria query = DetachedCriteria.forClass(JobProcInfo.class);
		if (softId!=null) {
			query.createAlias("clientSoft", "clientSoft").add(Restrictions.eq("clientSoft.id", softId));
		}else if (hardId!=null) {
			query.createAlias("clientDevice", "clientDevice").add(Restrictions.eq("clientDevice.id", hardId));
		}
		return this.findAllByCriteria(query);
	}

}
